<?php

namespace XWAM\Addon\PXCallSystem\Model;

/**
 * CDRModel.class数据库模型
 *
 * @author SoChishun <14507247@qq.com>
 * @version 1.0
 * @since 1.0.0 <2015-4-25> SoChishun Added.
 */
class CDRModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $tableName = 'cdr';

    /**
     * 字段定义
     * @var array
     */
    protected $fields = array();

    /**
     * 命名范围
     * @var array
     */
    protected $_scope = array(
        'export' => array(
            'field' => "callerNo as '主叫号码', dst as '被叫号码', extension as '工号', direction as '方向', xcallAnswer as '开始时间', end as '结束时间', billsec as '通话时长', amount as '金额', dtmf as '按键信息', ecdtmf as '评价按键'",
            'order' => 'id desc',
        ),
        'item_list' => array(
            'field' => 'id,siteID,callerNo,dst,extension,direction,xcallAnswer,end,billsec,amount,seatdtmf,dtmf,ecdtmf,recordingfile',
            'order' => 'id desc',
        ),
    );

    /**
     * 是否批量验证
     * @var boolean
     */
    protected $patchValidate = true;

    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array();

    /**
     * 自动填充
     * @var array
     */
    protected $_auto = array();

    /**
     * 返回列表
     * @return array
     * @xince 1.0 <2015-6-9> SoChishun Added.
     */
    public function get_cdr_list($where, $table) {
        $orderby = I('sort', $this->_scope['item_list']['order']) . ' ' . I('order');
        $page = I('page', 1) . ',' . I('rows', C('USER.PAGE_SIZE'));
        $total = $this->table($table)->where($where)->count('id');
        $list = $this->table($table)->scope('item_list')->where($where)->order($orderby)->page($page)->select();
        $out = array();
        foreach ($list as $row) {
            $row['directionText'] = get_channelDirection_text($row['direction']);
            $row['billsecText'] = sofn_seconds_format($row['billsec']);
            $row['siteIDText'] = get_siteID_text($row['siteID']);
            $row['listenPath']=get_voc_url('rec').parse_voc_path($row['recordingfile']);
            $row['downloadPath']=$row['listenPath'];
            $out[] = $row;
        }        
        $sum['amount'] = $this->table($table)->where($where)->sum('amount');
        $sum['billsec'] = $this->table($table)->where($where)->sum('billsec');
        $sum['end']='统计';
        return array('total' => $total, 'rows' => $out,'footer'=>array($sum));
    }

}
